<?php

if (!defined('BASEPATH'))
    exit('No direct script access allowed');

/**
 *
 * @version 1.0
 * @author xpl0it
 */
class Sdp extends CI_Controller {

    function __construct() {
        parent::__construct();
        $this->load->model('sdp_model');
        $this->load->model('pon_model');
//        $this->output->enable_profiler(TRUE);
    }

    function index($pon_id) {
        $this->checkauth();
        $data['query'] = $this->sdp_model->select_by_ponid($pon_id);
        $data['pon'] = $this->pon_model->select_by_filter($pon_id);

        $content = $this->load->view('sdp/index', $data, true);
        $content_data = new stdClass();
        $content_data->content = $content;
        $this->load->view('template/render_dt', $content_data);
    }

    function add($pon_id) {
        $this->checkauth();
        $data['pon'] = $this->pon_model->select_by_filter($pon_id);

        $content = $this->load->view('sdp/form', $data, true);
        $content_data = new stdClass();
        $content_data->content = $content;
        $this->load->view('template/render', $content_data);
    }

    function edit($pon_id, $param) {
        $this->checkauth();
        $data['pon'] = $this->pon_model->select_by_filter($pon_id);
        $data['val'] = $this->sdp_model->select_by_filter($param);
        
        $content = $this->load->view('sdp/form', $data, true);
        $content_data = new stdClass();
        $content_data->content = $content;
        $this->load->view('template/render', $content_data);
    }

    function save() {
        $this->checkauth();

        if ($_POST['sdp_id'] != '') {
            $rs = $this->sdp_model->update($this->input->post(NULL, TRUE));
            if ($rs) {
                $data['sdp_id'] = $_POST['sdp_id'];

                $this->load->view('template/header');
                $data = '<script type="text/javascript">alert("แก้ไขข้อมูลสำเร็จแล้ว !");location = "' . base_url("index.php/admin/sdp/index/{$_POST['pon_id']}") . '";</script>';
                echo $data;
            } else {
                $this->load->view('template/header');
                $data = '<script type="text/javascript">alert("ข้อมูลไม่ถูกต้อง ไม่สามารถแก้ไขข้อมูลได้ !");window.history.back();</script>';
                echo $data;
            }
        } else {
            $rs = $this->sdp_model->insert($this->input->post(NULL, TRUE));
            if ($rs) {
                $data['sdp_id'] = $rs;

                $this->load->view('template/header');
                $data = '<script type="text/javascript">alert("บันทึกข้อมูลสำเร็จแล้ว !");location = "' . base_url("index.php/admin/sdp/index/{$_POST['pon_id']}") . '";</script>';
                echo $data;
            } else {
                $this->load->view('template/header');
                $data = '<script type="text/javascript">alert("ข้อมูลไม่ถูกต้อง ไม่สามารถบันทึกข้อมูลได้ !");window.history.back();</script>';
                echo $data;
            }
        }
    }

    function del($id) {
        $this->checkauth();
        $rs = $this->sdp_model->delete($id);
        if ($rs) {
            $this->load->view('template/header');
            $data = '<script type="text/javascript">alert("ลบข้อมูล SDP สำเร็จแล้ว ! ");window.history.back();</script>';
            echo $data;
        }
    }

    private function checkauth() {
        if (!$this->session->userdata('authen')) {
            redirect('admin/login/', 'refresh');
        }
    }

}

/* End of file customer.php */
/* Location: ./application/controllers/admin/pon.php */